module.exports = Player;
let Card = require('./card.js');
let Utils = require('./utils.js');
let AI = require('./ai.js');
let log = require('../../util/log.js');

function Player(id, name, cid, uuid) {
    this._pid = id;
    this._name = name;
    this._cid = cid;
    this._uuid = uuid;
    this._cards = [];
    this._history = [];
    this._playCards = [];
    this._lastHands = {'id': id, 'cards': []};
    this._score = 0;
    this._stay = true;
    this._auto = false;
    this._aiLevel = 0;
    this._isRobot = false;
    this._showCard = false;
    this._double = 1;
}
Player.prototype.setCid = function (cid) {
    this._cid = cid;
};
Player.prototype.setUuid = function (uuid) {
    this._uuid = uuid;
};
Player.prototype.setStay = function (stay) {
    this._stay = stay;
};
Player.prototype.getPid = function () {
    return this._pid;
};
Player.prototype.getName = function () {
    return this._name;
};
//Player.prototype.getName = function () {
//    return 'player[' + this._pid + ']';
//};
Player.prototype.setCards = function (cc) {
    ////log.d('set cards:',cc);
    this._cards = cc;
    this._cards.sort(Card.compare);
    this._history = [];
    this._playCards = [];
    this._lastHands = {'id': this._pid, 'cards': []};
};
Player.prototype.setCoin = function (coin) {
    this._coin = coin;
};
Player.prototype.getCoin = function () {
    return this._coin;
};
Player.prototype.setScore = function (score) {
    this._score = score;
};
Player.prototype.getScore = function () {
    return this._score;
};
Player.prototype.setShowCard = function (showCard) {
    this._showCard = showCard;
};
Player.prototype.getShowCard = function () {
    return this._showCard;
};
Player.prototype.setDouble = function () {
    this._double = this._double * 2;
};
Player.prototype.getDouble = function () {
    return this._double;
};
Player.prototype.isDouble = function () {
    return this._double > 1;
};
Player.prototype.play = function (idxlist) {
    //log.d("play:", idxlist, this._cards);
    for (let i in idxlist) {
        let card = this._cards[idxlist[i]];
        card.valid = false;   // 出过的牌打上标记
        this._playCards.push(card);
    }
    //log.d('playCards:',this._playCards);
    //log.d('idx:',(this._cards.length - this._playCards.length));
    return this._cards.length - this._playCards.length;
};
// 设置为自动托管状态
Player.prototype.setAutoPlay = function (auto) {
    this._auto = auto;
    if (this._aiLevel == 0) {
        this._aiLevel = 1;
    }
};
// 设置为机器人
Player.prototype.setRobot = function (isRobot) {
    this._isRobot = isRobot;
};
Player.prototype.isAutoPlay = function (auto) {
    return this._auto;
};
Player.prototype.isRobot = function () {
    return this._isRobot;
};
//设置ai等级,0为最低,1为托管,2为智能,3为高智能
Player.prototype.setAiLevel = function (level) {
    this._aiLevel = level;
};
// 自动出牌,lastInfo为上一手出牌信息
Player.prototype.autoPlay = function (lastInfo) {
    let handCards = this.getHandCards();
    let ai = new AI(this._aiLevel);
    return ai.autoPlay(lastInfo, handCards);
};
Player.prototype.setDZ = function (dzCard) {
    this._cards = this._cards.concat(dzCard);
};

Player.prototype.sort = function () {
    this._cards.sort(Card.compare);

    //log.d(this._pid, '手牌:', this._cards);
};

Player.prototype.dump = function () {
    let x = [];
    for (let c in this._cards) {
        x.push(this._cards[c].getName())
    }
    return x.join(',')
};

// 返回指定的扑克牌
Player.prototype.getCardsIdx = function (idlist) {
    let idx = [];
    let ff = function (cards, id) {
        for (let j = 0; j < cards.length; j++) {
            if (cards[j]._id == id) {
                return j;
            }
        }
        return null;
    }
    for (let i in idlist) {
        let id = idlist[i];
        let x = ff(this._cards, id);
        if (x != null) {
            idx.push(x);
        } else {
            log.e("出牌错误. ID:" + JSON.stringify(id) + "已经出过了")
        }
    }
    return idx;
};
// 返回指定的扑克牌
Player.prototype.getCards = function (idxList) {
    let cs = [];
    for (let i in idxList) {
        let idx = idxList[i];
        let card = this._cards[idx];
        cs.push(card);
    }
    return cs;
};

// 返回扑克牌
Player.prototype.getAllCards = function () {
    return this._cards;
};

// 返回可用的牌
Player.prototype.getHandCards = function () {
    let handCards = [];
    for (let i in this._cards) {
        let c = this._cards[i];
        if (c.valid != false)
            handCards.push(c);
    }
    return handCards;
};

// 返回手上剩余牌的张数
Player.prototype.getCardsNum = function () {
    return this._cards.length - this._playCards.length;
};

// 保存上一次出的牌
Player.prototype.setLastHands = function (lastHands) {
    this._lastHands = lastHands;
};
// 读取上一次出的牌
Player.prototype.getLastHands = function () {
    return this._lastHands;
};

// 根据牌来决定叫分的水平
Player.prototype.askScoreLevel = function () {
    let p = {};
    for (let i = 0; i <= 14; i++) {
        p[i] = 0;
    }
    for (let i in this._cards) {
        let v = this._cards[i]._value;
        p[v]++;
        if (v == 13) {
            p[14]++;
        }
        if (v == 14) {
            p[13]++;
        }
    }

    let points = {};
    points[0] = {1: 60, 2: 105, 3: 180, 4: 300};
    points[1] = {1: 64, 2: 112, 3: 192, 4: 320};
    points[2] = {1: 68, 2: 119, 3: 204, 4: 340};
    points[3] = {1: 72, 2: 126, 3: 216, 4: 360};
    points[4] = {1: 76, 2: 133, 3: 228, 4: 380};
    points[5] = {1: 80, 2: 140, 3: 240, 4: 400};
    points[6] = {1: 84, 2: 147, 3: 252, 4: 420};
    points[7] = {1: 88, 2: 154, 3: 264, 4: 440};
    points[8] = {1: 92, 2: 161, 3: 276, 4: 460};
    points[9] = {1: 96, 2: 168, 3: 288, 4: 480};
    points[10] = {1: 100, 2: 175, 3: 300, 4: 500};
    points[11] = {1: 140, 2: 245, 3: 420, 4: 700};
    points[12] = {1: 180, 2: 315, 3: 540, 4: 900};
    points[13] = {1: 240, 2: 500};
    points[14] = {1: 400, 2: 500};

    let add = 0;
    for (let i in p) {
        if (p[i] == 0) {
            continue;
        }
        add += points[i][p[i]];
    }

    if (add >= 2000) {
        return 3;
    }
    if (add >= 1800) {
        return 2;
    }
    if (add >= 1400) {
        return 1;
    }
    return 0;
};
// 根据牌来决定叫地主
Player.prototype.askDzLevel = function () {
    let p = {};
    for (let i = 0; i <= 14; i++) {
        p[i] = 0;
    }
    for (let i in this._cards) {
        let v = this._cards[i]._value;
        p[v]++;
        if (v == 13) {
            p[14]++;
        }
        if (v == 14) {
            p[13]++;
        }
    }

    let points = {};
    points[0] = {1: 60, 2: 105, 3: 180, 4: 300};
    points[1] = {1: 64, 2: 112, 3: 192, 4: 320};
    points[2] = {1: 68, 2: 119, 3: 204, 4: 340};
    points[3] = {1: 72, 2: 126, 3: 216, 4: 360};
    points[4] = {1: 76, 2: 133, 3: 228, 4: 380};
    points[5] = {1: 80, 2: 140, 3: 240, 4: 400};
    points[6] = {1: 84, 2: 147, 3: 252, 4: 420};
    points[7] = {1: 88, 2: 154, 3: 264, 4: 440};
    points[8] = {1: 92, 2: 161, 3: 276, 4: 460};
    points[9] = {1: 96, 2: 168, 3: 288, 4: 480};
    points[10] = {1: 100, 2: 175, 3: 300, 4: 500};
    points[11] = {1: 140, 2: 245, 3: 420, 4: 700};
    points[12] = {1: 180, 2: 315, 3: 540, 4: 900};
    points[13] = {1: 240, 2: 500};
    points[14] = {1: 400, 2: 500};

    let add = 0;
    for (let i in p) {
        if (p[i] == 0) {
            continue;
        }
        add += points[i][p[i]];
    }

    if (add >= 1800) {
        return 1;
    }
    return 0;
};